<?php
namespace Home\Controller;
use Think\Controller;

class PublicController extends Controller {
    
    /**
     * 用户注册
     * @return [type] [description]
     */
    public function register() {
        $regType= M('system_config')->getByConfig('RegType');
        if($regType['value']!=0){
        	$userModel = D('User');
        	if(IS_POST) {
        		$data = $userModel->create();
        		if($data){
        			$data['username'] = 'm'.time();

        			$res = $userModel->add($data);
        			if($res) {
        				$dataGet['uid'] = $res;
        				$dataGet['mobile'] = I('post.mobile');
        				M('UserInfo')->data($dataGet)->add();

        				session('smsCode', null); 
        				session('smsTime', null);
        				session('smsMobile', null);

        				$this->success('注册成功');
        				exit;
        			} else {
        				$this->error('注册失败');
        			}
        		} else {
        			$this->error($userModel->getError());
        		}
        	}

        	$this->display();
        }else{
            $this->error("注册已关闭！");
        }
    }

    public function login() {
    	$userModel = D('User');
    	if(IS_POST) {
    		if($userModel->create($_POST, 4)){
    			$userinfo = session('userinfo');
    			if(!empty($userinfo) && $userinfo['uid']) {
    				$this->success('登录成功');
    				exit;
    			} else {
    				$this->error('登录失败');
    			}
    		} else {
    			$this->error($userModel->getError());
    		}
    	}


    	$this->display();
    }

    public function sendSMSCode() {
    	$type = I('type', '');
    	$mobile = I('mobile', '');

    	if(!$mobile) {
    		$message['status'] = -1;
    		$message['msg'] = '手机号不存在';
    		$this->ajaxReturn($message);
    	}

    	switch ($type) {
    		case 'register':
    			$m = M('UserInfo')->where(array('mobile' => $mobile))->getField('uid');
    			if($m) {
		    		$message['status'] = -2;
		    		$message['msg'] = '此号码已注册过';
		    		$this->ajaxReturn($message);	
    			}

    			$code = generateCode();
    			$res = sendSMS($mobile, $code);
    			// echo $code;
    			if($res) {
    				session('smsCode', $code); 
    				session('smsTime', time());
    				session('smsMobile', $mobile);
		    		$message['status'] = 1;
		    		$message['msg'] = '发送成功';
		    		$this->ajaxReturn($message);	

    			} else {
		    		$message['status'] = -3;
		    		$message['msg'] = '发送失败';
		    		$this->ajaxReturn($message);	
    			}
    			# code...
    			break;
    		
    		default:
	    		$message['status'] = -4;
	    		$message['msg'] = '不存在此类型';
	    		$this->ajaxReturn($message);		
    			break;
    	}
    }

    public function getLiveUrl($stream){
        if($stream){
            echo  authAliyun('http://jwlive.appwuhan.com/jw/'.$stream.'.m3u8');
        }else{
            return false;
        }
    }

    public function getLiveUrlp($stream){
        $callback =I('get.callback');
        if($stream){
            echo $callback . "('".authAliyun('http://jwlive.appwuhan.com/jw/'.$stream.'.m3u8')."')";
        }else{
            return false;
        }
    }

    public function func1(){
        echo 1;
    }
}